/********************************************************************************
 * Copyright 2001 - 2015 Comit. All Rights Reserved.
 * 作者：RockeyCai
 * 日期：2015-10-12 22:13:35
 * 描述：微博图片的映射模型。(自动生成，代码模块1.0v)
 * *****************************************************************************/
var async = require('async');
//var _ = require('underscore');
//var GlobalError = require('../../server/lib/GlobalError');
//var ResultListData = require('../../server/lib/ResultListData');
//var ResultJson = require('../../server/lib/ResultJson');
//var LogHelper = require('../../server/lib/LogHelper');
//var String = require('../server/lib/String');
//var utility = require('../../server/lib/Utility');
var cheerio = require('cheerio');
var superagent = require('superagent');
module.exports = function (PIC) {
  //var gooddriver_servicer_cloud = PIC.app.dataSources.gooddriver_servicer_cloud;


  PIC.getData = function (req, res) {

    var urlArray = new Array();
    for (var index = 1; index <= 11; index++) {
      var url = "http://photo.weibo.com/photos/get_all?uid=3117780635&album_id=3609172263467128&type=3&page=" + index + "&count=100&__rnd=1443098675033";
      urlArray.push(url);
    }

    //callback(null, item.name);
    async.concatSeries(urlArray, function (url, callback) {
      PIC.getDataWeb(url, callback);
    }, function (err, resluts) {
      if (err) {
        console.log(err);
        return res.send("err")
        res.end();
      }
      console.log(resluts);
      res.send("true");
      res.end();
    });

  }

  /**
   * 获取微博图片数据
   * @param req
   * @param res
   */
  PIC.getDataWeb = function (url, callback) {

    //var url = "http://photo.weibo.com/photos/get_all?uid=3117780635&album_id=3609172263467128&type=3&page=" + index + "&count=100&__rnd=1443098675033";
    superagent.get(url)
      .set("Cookie", "U_TRS1=000000d4.616a4b34.5422b3f3.544a9524; SINAGLOBAL=60.223.238.4_1418133330.931116; UOR=blog.sina.com.cn,blog.sina.com.cn,; ULV=1444412960398:19:1:1:122.13.132.204_1444404916.623228:1438618251978; vjuids=-40c315171.14a428e870c.0.04c2f734f69ce8; vjlast=1430063202.1444209398.11; SGUID=1418455994785_83093545; SUBP=0033WrSXqPxfM725Ws9jqgMF55529P9D9WF5P2doApvjuUv5W77mz62v; ALF=1476193350; sso_info=v02m6alo5qztKWRk5SlkJOMpZCjpKWRk5SlkKOMpZCjgZ-Jp5WpmYO0so6TnLmMs5y4jZOEsA==; U_TRS2=000000e5.57ace5e.561bb769.cb3bd47d; SUS=SID-2979378510-1444657350-GZ-id5g2-84372817ae3f3d5ea2a97b0dfea59e19; SUE=es%3D844e842c69fe2d7907125507af9a1983%26ev%3Dv1%26es2%3D40a6b35860dae1639b940edc6169d567%26rs0%3DboC55u9ztNXvg5hnIl8NdS7s4iOEmyz55wW73PGQPsci8Lf29R4qzgzS%252F5ysyudcIssQ3cGJL%252BQTBKeww3V1DS6shzMDVi5vJcunVLyqbb7GGyopj0ttRap%252Fzw1RDF1fHIG5lfgQ25fC1gjyHsUcOtNpXwiJVU2qF78JVBG0LIY%253D%26rv%3D0; SUP=cv%3D1%26bt%3D1444657350%26et%3D1444743750%26d%3D40c3%26i%3D9e19%26us%3D1%26vf%3D0%26vt%3D0%26ac%3D2%26st%3D0%26lt%3D7%26uid%3D2979378510%26user%3D13928720730.cn%26ag%3D8%26name%3D13928720730%2540sina.cn%26nick%3D%25E5%25A3%25B9%25E5%25B3%25B0_%26sex%3D1%26ps%3D0%26email%3D13928720730%2540sina.cn%26dob%3D%26ln%3D%26os%3D%26fmp%3D%26lcp%3D2015-06-03%252012%253A54%253A47; SUB=_2A257H8iWDeTxGeRH7FsS9ybJyjyIHXVYbL1erDV_PUNbvtBeLWTjkW9k4Lv_MgUIq7hpbE8szgJA_Cl8IQ..")
      .end(function (err, sres) {
        if (err) {
          console.log(err)
          return callback(null, null);
        }

        var picArray = new Array();

        var result = JSON.parse(sres.text);

        for (var i = 0; i < result.data.photo_list.length; i++) {
          var photo = result.data.photo_list[i]
          //console.log("photo_id:" + photo.photo_id);
          //console.log("pic_name:" + photo.pic_name);
          //console.log("album_id:" + photo.album_id);
          var model = {
            PHOTO_ID: photo.photo_id,
            PIC_NAME: photo.pic_name,
            ALBUM_ID: photo.album_id
          };
          picArray.push(model);
        }

        async.concatSeries(picArray, function (model, cb) {
          PIC.create(model , function(error , rModel){
            if (error) {
              console.log(error);
              console.log("保存错误");
            }else{
              console.log("保存成功");
            }
            cb(null,null)
          });
        }, function (err, resluts) {
          if (err) {
            console.log(err);
          }
          callback(null, null);
        });



      });

  }

  /**
   * 添加微博图片对象
   * @param req
   * @param res
   */
  PIC.addModel = function (req, res) {

    var PIC_NAME = req.params.PIC_NAME || req.query.PIC_NAME;
    if (_.isEmpty(PIC_NAME)) {
      return GlobalError.throwErrorMsg("PIC_NAME名称，带后缀必填", res);
    }
    var ALBUM_ID = req.params.ALBUM_ID || req.query.ALBUM_ID;
    if (ALBUM_ID == undefined || ALBUM_ID <= 0) {
      return GlobalError.throwErrorMsg("ALBUM_ID相册id必填", res);
    }
    var model = {};
    model.PHOTO_ID = (new Date()).valueOf();//主键
    model.PIC_NAME = PIC_NAME;//名称，带后缀
    model.ALBUM_ID = ALBUM_ID;//相册id
    //保存记录
    this.create(model, function (err, obj) {
      if (err) {
        return GlobalError.throwErrorMsg(err, res);
      }
      return ResultJson.ResultJson(res, obj);
    });
  }
}
